package hibernate.web;

import hibernate.fileControler.MyException;
import hibernate.model.Duyurular;
import hibernate.model.Sirketprofil;
import hibernate.service.EmmyService;

import java.util.Collections;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ComponentSystemEvent;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@SuppressWarnings("serial")
@Component("DuyurularBean")
@Scope("session")
public class DuyurularBean implements BeanService {

	@Autowired
	private transient EmmyService emmyService;
	
	private Duyurular selectedDuyuru;	
	private List<Duyurular> duyuruList;
	
	private TemsilciBean temBean;
	private SirketprofilBean sirketProBean;
	
	private boolean duyuruIslem= false;
	private String TarihBitis;
	
	
	@SuppressWarnings("unchecked")
	@Override
	@PostConstruct
	public void init(){		
		duyuruList = (List<Duyurular>) emmyService.findAll(new Duyurular());
		Collections.reverse(duyuruList);
	}	
	
	@Override
	public String restart(){
		deleteSelectedDuyuru();
		deleteSelectedSirket();
		init();
		return "";
	}
	
	@Override
	public String deleteView(){
		emmyService.delete(selectedDuyuru);
		return "";
	}

	@Override
	public String viewNew() {
		temBean = (TemsilciBean) emmyService.getBean(new TemsilciBean());
		selectedDuyuru= new Duyurular();
		selectedDuyuru.setTemsilci(temBean.getCurrentTemsilci());
		selectedDuyuru.setSirketprofil(temBean.getCurrentTemsilci().getSirketprofil());
		duyuruIslem(true);
		return "";
	}

	@Override
	public String viewDetail() {/*
		List<Duyurular> valueList = (List<Duyurular>) duyuruTable.getValue();	
		System.out.println("table size : "+valueList.size());		
		Iterator<SimpleRowKey> keys = duyuruTable.getSelection().getKeys();
		
		if(keys.hasNext()){
			currentDuyurular= valueList.get(keys.next().intValue());
			System.out.println("dolu = "+currentDuyurular.getDuyuruid());
			return "detail";
		}else{
			System.out.println("bo�ta");
			return null;
		}*/ return"detay";
	}

	@Override
	public String SaveOrUpdate() throws MyException {	
		temBean = (TemsilciBean) emmyService.getBean(new TemsilciBean());
		selectedDuyuru.setTemsilci(temBean.getCurrentTemsilci());
		emmyService.saveOrUpdate(selectedDuyuru);
		return "list";
	}

	@SuppressWarnings("unchecked")
	public void getSirketDuyuru(Sirketprofil sp){
		duyuruList = (List<Duyurular>) emmyService.getQuery("findDuyuruFirmaid", "dFirmaid", sp.getFirmaid());
	}
	
	@SuppressWarnings("unchecked")
	public String getDetayliDuyuru(){		
		duyuruList = (List<Duyurular>) emmyService.getQuery(createQuery());
		return "list";
	}
	
	public void deleteSelectedSirket(){ 
		sirketProBean = (SirketprofilBean)emmyService.getBean(new SirketprofilBean());
		sirketProBean.deleteSelectedSirket();
	}
	
	public void deleteSelectedDuyuru(){
		selectedDuyuru = null;
	}
	
	public void duyuruIslem(boolean islem){
		 FacesMessage msg = null; 
			if(islem){
				if(selectedDuyuru !=null){
					duyuruIslem = islem;
				}else{
					duyuruIslem =true;
					msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Seçim yapılmadı","Yeni Kayıt yapılacak");
					FacesContext.getCurrentInstance().addMessage(null, msg);
					viewNew();
				}
			}else{
				duyuruIslem = islem;
			}
	}
	
	public String verifyProducer(ComponentSystemEvent event){
		try{
			System.out.println("Duyuru verifyingProducer");
			sirketProBean = (SirketprofilBean) emmyService.getBean(new SirketprofilBean());
			getSirketDuyuru(sirketProBean.getCurrentSirketpro());
			sirketProBean.setSelectedSirket(sirketProBean.getCurrentSirketpro());
			System.out.println("Duyuru verifyProducer ok");
			return "";
		}catch(Exception e){
			System.out.println("duyuru işlem sayfası erişim hatası: "+e);
			return "index";
		}
	}
	
	@Override
	public String createQuery() {
		Duyurular dyr  = selectedDuyuru;		
		String query = "from Duyurular";				
		
		if(dyr == null){
			return query;
		}
		if(dyr.getSirketprofil().getFirmaid() != 0){
			if(query.indexOf("where") != -1){
				query += " and firmaid="+dyr.getSirketprofil().getFirmaid()+"";
				
			}
			else{
				query += " where firmaid="+dyr.getSirketprofil().getFirmaid()+"";			
			}
		}
		if(dyr.getTemsilci().getKullaniciid() != 0){
			if(query.indexOf("where") != -1){
				query += " and kullaniciid="+dyr.getTemsilci().getKullaniciid()+"";
				
			}
			else{
				query += " where kullaniciid="+dyr.getTemsilci().getKullaniciid()+"";			
			}
		}
		if(dyr.getBaslik() != null){
			if(query.indexOf("where") != -1){
				query += " and baslik like '%"+dyr.getBaslik()+"%'";
				
			}
			else{
				query += " where baslik like '%"+dyr.getBaslik()+"%'";			
			}
		}
		if(dyr.getAciklama() != null){
			if(query.indexOf("where") != -1){
				query += " and aciklama like '%"+dyr.getAciklama()+"%'";
				
			}
			else{
				query += " where aciklama like '%"+dyr.getAciklama()+"%'";			
			}
		}
		if(dyr.getOnemDerecesi() != 0){
			if(query.indexOf("where") != -1){
				query += " and onemDerecesi="+dyr.getOnemDerecesi()+"";
				
			}
			else{
				query += " where onemDerecesi="+dyr.getOnemDerecesi()+"";			
			}
		}
		if(dyr.getKimlericin() != 0){
			if(query.indexOf("where") != -1){
				query += " and kimlericin="+dyr.getKimlericin()+"";
				
			}
			else{
				query += " where kimlericin="+dyr.getKimlericin()+"";			
			}
		}
		if(dyr.getTarih() != null){
			if(query.indexOf("where") != -1){
				query += " and (tarih between "+dyr.getTarih()+" and "+TarihBitis+")";
				
			}
			else{
				query += " where (tarih between "+dyr.getTarih()+" and "+TarihBitis+")";			
			}
		}
		return query;
	}	

	@Override
	public void getSession() {
		// TODO Auto-generated method stub
		
	}

	public Duyurular getSelectedDuyuru() {
		return selectedDuyuru;
	}

	public void setSelectedDuyuru(Duyurular selectedDuyuru) {
		this.selectedDuyuru = selectedDuyuru;
	}

	public List<Duyurular> getDuyuruList() {
		return duyuruList;
	}

	public void setDuyuruList(List<Duyurular> duyuruList) {
		this.duyuruList = duyuruList;
	}

	public String getTarihBitis() {
		return TarihBitis;
	}

	public void setTarihBitis(String tarihBitis) {
		TarihBitis = tarihBitis;
	}

	public TemsilciBean getTemBean() {
		return temBean;
	}

	public void setTemBean(TemsilciBean temBean) {
		this.temBean = temBean;
	}

	public boolean isDuyuruIslem() {
		return duyuruIslem;
	}

	public void setDuyuruIslem(boolean duyuruIslem) {
		this.duyuruIslem = duyuruIslem;
	}

	public SirketprofilBean getSirketProBean() {
		return sirketProBean;
	}

	public void setSirketProBean(SirketprofilBean sirketProBean) {
		this.sirketProBean = sirketProBean;
	}
}